﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;

namespace EC.Admin.Controllers
{
    using EC.Application.Interface;

    using EC.Entity;
    using EC.Entity.Enum;
    using EC.Entity.Query;
    using EC.Entity.Request.DepositOrder;
    using EC.Util;

    /// <summary>
    /// 存款
    /// </summary>
    public class DepositOrderController : BaseController
    {
        #region 注入
        /// <summary>
        /// 存款订单数据接口
        /// </summary>
        private readonly DepositOrderService _depositOrderService;

        /// <summary>
        /// 用户组数据接口
        /// </summary>
        private readonly UserGroupService _userGroupService;

        /// <summary>
        /// 商户数据接口
        /// </summary>
        private readonly MerchantService _merchantService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="labelService"></param>
        public DepositOrderController(UserGroupService userGroupService, DepositOrderService depositOrderService, MerchantService merchantService, LoginService loginService) : base(loginService)
        {
            _depositOrderService = depositOrderService;
            _userGroupService = userGroupService;
            _merchantService = merchantService;
        }

        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.DepositOrder0001)]
        public IActionResult Index()
        {
            ViewBag.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

            ViewBag.YesterdayStartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
            ViewBag.YesterdayEndTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 23:59:59");

            ViewBag.MonthStartTime = DateUtil.FirstDayOfMonth(DateTime.Now).ToString("yyyy-MM-dd 00:00:00");
            ViewBag.MonthEndTime = DateUtil.LastDayOfMonth(DateTime.Now).ToString("yyyy-MM-dd 23:59:59");


            ViewBag.ProxyGroup = _merchantService.GetType((int)MerchantType.卡商);

            var merchantIds = new List<int>() { 0 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    merchantIds.Add(Convert.ToInt32(item));
                }
            }

            ViewBag.Merchants = _merchantService.GetMerchantByIds(merchantIds);
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>

        private bool IsDate(string date)
        {
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 补单
        /// <summary>
        /// 补单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AuthFilter(AuthCode.DepositOrder0002)]
        public JsonResult SupplementOrder(int id)
        {
            var response = _depositOrderService.Success(id,UserId);

            return Json(response);
        }
        #endregion

        #region 撤销
        /// <summary>
        /// 撤销
        /// </summary>
        /// <returns>视图</returns>
        [HttpPost]
        [AuthFilter(AuthCode.DepositOrder0003)]
        public JsonResult Revoke(int id)
        {
            var response = _depositOrderService.Revoke(new RevokeRequest() { 
                Id = id,
                UserId = UserId
            });

            return Json(response);
        }
        #endregion

        #region 重置
        /// <summary>
        /// 重置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AuthFilter(AuthCode.DepositOrder0003)]
        public JsonResult Reset(int id)
        {
            var response = _depositOrderService.Reset(id);

            return Json(response);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 收款订单
        /// </summary>
        /// <returns>JResult</returns>
        public JsonResult DepositOrderQuery(DepositOrderQuery query)
        {
            query.MerchantIds = new List<int>() { -1 };
            query.UserPermission = UserPermissions;

            if (query.MerchantId.HasValue)
            {
                query.MerchantIds.Add(query.MerchantId.Value);
            }
            else
            {
                //获取商户
                var merchant = _merchantService.Get(MerchantId);
                //验证
                if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
                {
                    var ids = merchant.ProxyId.Split(',');
                    foreach (var item in ids)
                    {
                        query.MerchantIds.Add(Convert.ToInt32(item));
                    }
                }
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _depositOrderService.Pager(query);

            return Json(list);
        }

        /// <summary>
        /// 收款订单导出
        /// </summary>
        /// <returns>PagedList<?></returns>
        public JsonResult DepositOrderExport(DepositOrderQuery query)
        {
            query.UserPermission = UserPermissions;
            query.Page = 1;
            query.Rows = 500;
            query.MerchantIds = new List<int>() { -1 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    query.MerchantIds.Add(Convert.ToInt32(item));
                }
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _depositOrderService.ExportPager(query);
            list.StatusCode = $"{query.StartTime}-{query.EndTime}";
            return Json(list);
        }
        #endregion
    }
}
